﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity;
using System.Web.Mvc;
using YCMS.Domain.Abstract;
using YCMS.Domain.Concrete;
using YCMS.Domain.Entities;
using YCMS.Models;
using PagedList;

namespace YCMS.Controllers
{
    public class MemberController : Controller
    {
        private IDockRepository dockRepository;
        private IMemberRepository memberRepository;

        public MemberController(IDockRepository repository, IMemberRepository memberRepository)
        {
            EfDbContext context = new EfDbContext();
            this.dockRepository = repository;
            this.dockRepository.Context = context;
            this.memberRepository = memberRepository;
            this.memberRepository.Context = context;
        }

        public ViewResult List(string sortOrder, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.MemberNameSortOrder = String.IsNullOrEmpty(sortOrder) ? "MemberName desc" : "";
            ViewBag.MemberNumberSortOrder = sortOrder == "MemberNumber" ? "MemberNumber desc" : "MemberNumber";
            ViewBag.PageNumber = page;

            MemberListViewModel memberListViewModel = new MemberListViewModel();
            switch (sortOrder)
            {
                case "MemberName desc":
                    memberListViewModel.Members = memberRepository.Members.OrderByDescending(m => m.LastName);
                    break;
                case "MemberName":
                    memberListViewModel.Members = memberRepository.Members.OrderBy(m => m.LastName);
                    break;
                case "MemberNumber desc":
                    memberListViewModel.Members = memberRepository.Members.OrderByDescending(m => m.MemberNumber);
                    break;
                default:
                    memberListViewModel.Members = memberRepository.Members.OrderBy(m => m.LastName);
                    break;


            }
            int pageSize = 10;
            int pageIndex = (page ?? 1) - 1;
            memberListViewModel.Members = memberListViewModel.Members.ToPagedList(pageIndex, pageSize);
            memberListViewModel.PageNumber = page;
            memberListViewModel.SortOrder = sortOrder;
            return View(memberListViewModel);
        }


        public ActionResult Details(int id, string sortOrder, int? page)
        {
            Member member = memberRepository.GetMemberByID(id);
            SelectList list = PopulateMemberDocks(member);
            MemberListViewModel viewModel = new MemberListViewModel();
            viewModel.MembersDocks = list;
            viewModel.Member = member;
            viewModel.PageNumber = page;
            viewModel.SortOrder = sortOrder;
            return View(viewModel);
        }

        private SelectList PopulateMemberDocks(Member member)
        {
            var memberDockQuery = from m in member.Docks
                                  select m.DockName;
            SelectList list = new SelectList(memberDockQuery);
            return list;
        }

       

        public ActionResult Edit(int id, string sortOrder, int? page)
        {
            Member member = memberRepository.GetMemberByID(id);

            MemberListViewModel viewModel = new MemberListViewModel();
            var selectedDocks = from m in member.Docks
                                select m.ID;
            List<int> list = selectedDocks.ToList();
            viewModel.AllDocks = PopulateDockDropDownList(list);
            viewModel.Member = member;
            viewModel.PageNumber = page;
            viewModel.SortOrder = sortOrder;
            return View(viewModel);
        }

        private MultiSelectList PopulateDockDropDownList(IEnumerable<int> selectedDocks)
        {

            var dockQuery = from d in dockRepository.Docks
                              orderby d.DockName
                              select d;

            MultiSelectList list = new MultiSelectList(dockQuery, "ID", "DockName", selectedDocks);
            return(list);
        }


        [HttpPost]
        public ActionResult Edit(MemberListViewModel model)
        {
            Member member = model.Member;
            try
            {
                if (ModelState.IsValid)
                {
                    Member memberToUpdate = memberRepository.GetMemberByID(member.ID);
                    memberToUpdate.FirstName = member.FirstName;
                    memberToUpdate.LastName = member.LastName;
                    memberToUpdate.MemberNumber = member.MemberNumber;

                    List<Dock> docksToRemove = new List<Dock>();
                    foreach (var dock in memberToUpdate.Docks)
                    {
                        docksToRemove.Add(dock);
                    }
                    foreach (var dock in docksToRemove)
                    {
                        memberToUpdate.Docks.Remove(dock);
                    }
                    foreach (var dockID in model.SelectedDocks)
                    {
                        Dock dock = dockRepository.GetDockById(dockID);
                        memberToUpdate.Docks.Add(dock);
                    }

                    memberRepository.UpdateMember(memberToUpdate);
                    memberRepository.Save();
                    return RedirectToAction("List", "Member", new { sortOrder = model.SortOrder, page = model.PageNumber });
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes.  Try again later");
            }
            return View(member);
        }


    }
}
